<template>
  <Panel>
    <template v-slot:header>
      <div>账号添加</div>
    </template>
    <template #con>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pwd">
          <el-input
            type="password"
            v-model="ruleForm.pwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户组" prop="selectVal">
          <el-select v-model="ruleForm.selectVal" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >添加</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
  </Panel>
</template>

<script>
import { validateUsername, validatePwd } from "../../utils/validata.js";
import { addUser } from "../../api/user";
export default {
  data() {
    var validateSelct = (rlue, value, callback) => {
      if (value === "") {
        callback(new Error("请选择用户组"));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        username: "",
        pwd: "",
        selectVal: "",
      },
      options: [
        { label: "普通管理员", value: "普通管理员" },
        { label: "超级管理员", value: "超级管理员" },
      ],
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        pwd: [{ validator: validatePwd, trigger: "blur" }],
        selectVal: [{ validator: validateSelct, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const data = await addUser({
            account: this.ruleForm.username,
            password: this.ruleForm.pwd,
            userGroup: this.ruleForm.selectVal,
          });
          if (data.code === 0) {
            this.$message({
              message: data.msg,
              type: "success",
            });
            this.$router.push("/account/list");
          } else {
            this.$message({
              message: data.msg,
              type: "error",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.el-form {
  width: 400px;
  padding: 20px;
}
</style>